package com.code.time1125;

import java.util.Arrays;
import java.util.concurrent.ThreadLocalRandom;

public class Test{ 
	public static void main(String[] args) {
		int[] arr = {5,2,2,1,5,8};
//		System.out.println(getMin(arr));
//		System.out.println(Arrays.toString(arrReverse(arr)));
		int[] oldArr = {1,3,4,5,6,6,0,5,6,7,0,5};
//		System.out.println(Arrays.toString(getNewArr(oldArr)));
		System.out.println(Arrays.toString(arrMerge(oldArr,arr)));
		System.out.println(getCode(6));
	}
	/**
	 * 第一题
	 * @param arr
	 * @return
	 * 1、定义一个方法：返回一个int[]中最小的那个数（5分）
	 */
	public static int getMin(int[] arr){
		Arrays.sort(arr);
		return arr[0];
	}
	/**
	 * 翻转数组
	 * @param arr
	 * @return
	 * 2、写一个方法，将一个给定的整型数组，将其元素反转，并且返回  
		例如： 源数组 8 7 3 4 5 6	倒转之后数组 6 5 4 3 7 8  （5分）

	 */
	public static int[] arrReverse(int[] arr){
		for (int i = 0,j = arr.length/2; i < j; i++) {
			int k = j*2-i-1;
			arr[i] = arr[i]^arr[k];
			arr[k] = arr[i]^arr[k];
			arr[i] = arr[i]^arr[k];
		}
		return arr;
	}
	/**
	 * @param arr
	 * @return
	 * 
	 *  3、现有如下的数组
		  int[] oldArr = {1,3,4,5,6,6,0,5,6,7,0,5};
		     取出以上数组不为0的值，然后将不为0的值存入一个新的数组，生成新的数组为：
		  int[] newArr = {1,3,4,5,6,6,5,6,7,5};	（5分）
	 */
	public static int[] getNewArr(int[] arr){
		int[] copyOf = Arrays.copyOf(arr, arr.length);
		int num = 0;
		for (int i = 0,j = arr.length;i < j; i++) {
			if(arr[i] == 0){
				num++;
			}else{
				copyOf[i-num] = arr[i];
			}
		}
		return Arrays.copyOf(copyOf, arr.length-num);
	}
	/**
	 * @param arr
	 * @return
	 */
	public static int[] arrMerge(int[] arr1,int[] arr2){
		int j = arr2.length,k = arr1.length;
		int[] copyOf = Arrays.copyOf(arr1, j+k);
		for (int i = 0; i < j; i++) {
			copyOf[k+i] = arr2[i];
		}
		Arrays.sort(copyOf);
		return copyOf;
	}
	
	/**
	 * @author Administrator
	 * 6、写2个单例模式，不考虑线程安全问题（10分，一个5分）
	 */
	public static class Singleton{
		private static Singleton single = new Singleton();

		private Singleton() {

		}
		
		public static Singleton getInstance(){
			return single;
		}
		
	}
	public static class Singleton2{
		private static Singleton2 single;
		
		private Singleton2() {
			
		}
		
		public static Singleton2 getInstance(){
			if(single == null) single = new Singleton2();
			return single;
		}
		
	}
	public static String getCode(int n){
		if(n<=0) return "";
		String string = "1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM",str="";
		for (int i = 0; i < n; i++) {
			str += string.charAt(getRandom(0,string.length()));
		}
		return str;
	}
	public static int getRandom(int start,int end){
		ThreadLocalRandom threadLocalRandom = ThreadLocalRandom.current();
		return threadLocalRandom.nextInt(start, end);
	}
	
}

